package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.ImagePiece;
import flc.ast.databinding.ActivityBoxGridBinding;
import flc.ast.utils.ImageSplitter;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class BoxGridActivity extends BaseAc<ActivityBoxGridBinding> {

    public static String imgPath = "";
    private Bitmap mImgBitmap;
    private List<ImagePiece> pictureSplitList=new ArrayList<>();
    private Dialog myTipDialog;



    @Override
    protected int onCreate() {
        return R.layout.activity_box_grid;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivBoxGridBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivBoxGridSave.setOnClickListener(this);
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivBoxGridSave:
                saveImg();
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        setImg();
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.ivPicCutImg.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                emitter.onNext(resultBit);
            }

            @Override
            public void accept(Bitmap bitmap) {
                if (bitmap != null) {
                    pictureSplitList = ImageSplitter.split(bitmap, 3, 3);
                    for (ImagePiece imagePiece : pictureSplitList) {
                        //保存到相册
                        ImageUtils.save2Album(imagePiece.bitmap, Bitmap.CompressFormat.PNG);
                    }
                    ToastUtils.showShort(R.string.save_suc);
                    startActivity(HomeActivity.class);
                }
                dismissDialog();
            }
        });
    }

    //设置图片
    private void setImg() {
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.ivPicCutImg.setImageBitmap(mImgBitmap);
        mDataBinding.ivPicCutImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicCutImg.setScaleEnabled(false);// 禁用缩放
        mDataBinding.ivPicCutImg.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.ivPicCutImg.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        TextView right = inflate.findViewById(R.id.ivDialogTipRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }


}